package com.takeshi.landlords.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class DoubleLinkedList<E> extends AbstractDoubleList<E> {
    private Node<E> first;
    private Node<E> last;

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public void foreach(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            action.accept(node.elemnt);
            node = node.next;
        }
    }

    @Override
    public E find(Predicate<? super E> predicate) {
        Objects.requireNonNull(predicate);
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (predicate.test(node.elemnt)) {
                return node.elemnt;
            }
            node = node.next;
        }
        return null;
    }

    @Override
    public E findPrev(Predicate<? super E> predicate) {
        Objects.requireNonNull(predicate);
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (predicate.test(node.elemnt)) {
                return node.getPrev().elemnt;
            }
            node = node.next;
        }
        return null;
    }

    @Override
    public E findNext(Predicate<? super E> predicate) {
        Objects.requireNonNull(predicate);
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (predicate.test(node.elemnt)) {
                return node.getNext().elemnt;
            }
            node = node.next;
        }
        return null;
    }

    @Override
    public E get(int index) {
        return node(index).elemnt;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == size) {
            Node<E> oldlast = last;
            last = new Node<E>(oldlast, element, first);//双向链表这里的最后一个节点指向Null，但是双向循环链表这个节点就指向了first
            if (oldlast == null) {
                //原来是空
                first = last;
                first.next = first;
                first.prev = first;//自己的下一个指向自己，自己的上一个指向自己
            } else {
                oldlast.next = last;
                first.prev = last;
            }
        } else {
            Node<E> next = node(index); //当前的index在等会添加完之后是新添加元素的next
            Node<E> prev = next.prev;
            Node<E> node = new Node<E>(prev, element, next);
            next.prev = node;
            prev.next = node;

            if (next == first) {//index==0) {
                first = node;
            }
        }
        //特殊情况，添加到0或者last
        size++;

    }

    @Override
    public E remove(int index) {
        Node<E> node = node(index);
        Node<E> prev = node.prev;
        Node<E> next = node.next;
        prev.next = next;
        next.prev = prev;
        if (node == first) {//index==0
            first = next;
        }
        if (node == last) {//index==size-1
            last = prev;
        }
        size--;
        return node.elemnt;
    }

    @Override
    public int indexOf(E element) {
        Node<E> tmpNode = first;
        for (int i = 0; i < size; i++) {
            if (element.equals(tmpNode.elemnt)) {
                return i;
            }
            tmpNode = tmpNode.next;
        }
        return ELEMENT_NOT_FOUND;

    }

    @Override
    public List<E> toList() {
        ArrayList<E> list = new ArrayList<>();
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            list.add(node.elemnt);
            node = node.next;
        }
        return list;
    }

    //Node类型，
    private Node<E> node(int index) {
        rangeCheck(index);
        Node<E> node = null;
        if (index < (size >> 1)) {
            node = first;//first是第一个node的引用，
            for (int i = 0; i < index; i++) {
                node = node.next;//找到第i个node
            }
        } else {
            node = last;//first是第一个node的引用，
            for (int i = size - 1; i > index; i--) {
                node = node.prev;//找到第i个node
            }
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder("[");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append(node.elemnt);
            node = node.next;

        }
        string.append("]");
        return string.toString();
    }

}